package com.alipay.app.groupmeal.standard.ui.c;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.base.BaseMVCActivity;
import com.alipay.app.groupmeal.standard.event.EventManager;
import com.alipay.app.groupmeal.standard.ui.c.abcp.CPayingAbcpActivity;
import com.alipay.app.groupmeal.standard.ui.c.pre.UnpaidPre;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * 未支付页面-补缴
 */
public class CUnpaidActivity extends BaseMVCActivity {

    private TextView mTvNotPaidNum;
    private TextView mTvAmount;
    private ImageView mIvQrcode;
    private TextView mBtnPaid;
    private LinearLayout mLlCancel;
    private TextView mTvCloseTime;
    private TextView mTvTips;
    private RelativeLayout mRlRoot;

    private float amount = 0;
    private final int closeSec = 60;
    private String outTradeNo = "";

    Disposable mdDisposable;

    //C屏组件版本要用到的未支付UI
    private UnpaidPre mUnpaidPre;

    public static void startAct(Context context, float amount, String outTradeNo) {
        Intent intent = new Intent(context, CUnpaidActivity.class);
        intent.putExtra("amount", amount);
        intent.putExtra("outTradeNo", outTradeNo);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_card;
    }

    @Override
    protected void initView() {
        mRlRoot = findViewById(R.id.rl_root);
        mTvNotPaidNum = findViewById(R.id.tv_not_paid_num);
        mTvAmount = findViewById(R.id.tv_amount);
        mIvQrcode = findViewById(R.id.iv_qrcode);
        mBtnPaid = findViewById(R.id.btn_paid);
        mLlCancel = findViewById(R.id.ll_cancel);
        mTvCloseTime = findViewById(R.id.tv_close_time);
        mTvTips = findViewById(R.id.tv_tips);
    }

    @Override
    protected void initData() {
        if (getIntent() != null) {
            amount = getIntent().getFloatExtra("amount", 0);
            outTradeNo = getIntent().getStringExtra("outTradeNo");
        }
    }

    @Override
    public void initPresentation() {
        super.initPresentation();
        if (mMainDisplay != null) {
            mUnpaidPre = new UnpaidPre(CUnpaidActivity.this, mMainDisplay);
            registerPresentation(mUnpaidPre);
        }
    }

    @Override
    public void onBusiness() {
        super.onBusiness();
        if (TextUtils.isEmpty(outTradeNo) || amount < 0) {
            finish();
        }
        mTvAmount = mUnpaidPre.getTvAmount();
        mBtnPaid = mUnpaidPre.getBtnPaid();
        mTvNotPaidNum = mUnpaidPre.getTvNotPaidNum();
        mIvQrcode = mUnpaidPre.getIvQrcode();
        mLlCancel = mUnpaidPre.getLlCancel();
        mTvCloseTime = mUnpaidPre.getTvCloseTime();
        mTvTips = mUnpaidPre.getTvTips();
        showPresentationAtMain(mUnpaidPre);
        mRlRoot.setVisibility(View.GONE);
        mBtnPaid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                payResult(true);
            }
        });
        mLlCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                payResult(false);
            }
        });
        mTvTips.setText("使用支付宝扫一扫\n左侧二维码");
        mTvAmount.setText(amount + "");

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //更新云售卖小程序appid
                String url = "alipays://platformapi/startapp?appId=2021003115693381" +
                        "&page=pages/home/index?outTradeNo=" + outTradeNo;
                mIvQrcode.setImageBitmap(QRCodeEncoder.syncEncodeQRCode(url, 300));
            }
        });

        mdDisposable = Flowable.intervalRange(0, closeSec, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        String btnText = (closeSec - aLong) + "秒";
                        mTvCloseTime.setText(btnText);
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        finish();
                    }
                })
                .subscribe();
    }

    @Override
    protected void dissmissPresnetation() {
        if (mUnpaidPre != null)
            mUnpaidPre.dismiss();
    }

    @Override
    protected void onDestroy() {
        if (mdDisposable != null && !mdDisposable.isDisposed()) {
            mdDisposable.dispose();
            mdDisposable = null;
        }
        if (CPayingAbcpActivity.payingActivity != null) {
            CPayingAbcpActivity.payingActivity.finish();
            EventBus.getDefault().post(new EventManager.clearAmount());
        }
        super.onDestroy();
    }

    /**
     * @param paid 是否已支付
     */
    public void payResult(boolean paid) {
        if (paid) {
            setResult(RESULT_OK);
        } else {
            setResult(RESULT_CANCELED);
        }
        finish();
    }

}
